package com.ctg.itrdc.imageconsole.control.ssh.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctg.itrdc.imageconsole.control.ssh.utils.DBUtils;
import com.ctg.itrdc.imageconsole.data.entity.ClusterConfig;

public class ClusterConfigDB {

	private static Logger logger = LoggerFactory.getLogger(ClusterConfigDB.class);

	private ClusterConfigDB() {
	}

	public static ClusterConfig getClusterConfig(Long createUserId, String clusterCode) {
		ClusterConfig clusterConfig = null;
		Connection con = null;
		try {
			con = DBUtils.getConn();
			PreparedStatement stmt = null;
			if (createUserId == null) {
				stmt = con
						.prepareStatement("select * from cluster_config where cluster_code=?");
				stmt.setString(1, clusterCode);
			} else {
				stmt = con
						.prepareStatement("select * from cluster_config where create_user_id=? and cluster_code=?");
				stmt.setLong(1, createUserId);
				stmt.setString(2, clusterCode);
			}
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				clusterConfig = new ClusterConfig();
				clusterConfig.setId(rs.getLong("id"));
				clusterConfig.setClusterCode(rs.getString("cluster_code"));
				clusterConfig.setClusterName(rs.getString("cluster_name"));
				clusterConfig.setClusterDesc(rs.getString("cluster_desc"));
				clusterConfig.setApiServerUrl(rs.getString("api_server_url"));
				clusterConfig.setMonitorUrl(rs.getString("monitor_url"));
				clusterConfig.setTengineClusterCode(rs.getString("tengine_cluster_code"));
				clusterConfig.setState(rs.getString("state"));
				clusterConfig.setCreateUserId(rs.getLong("create_user_id"));
				clusterConfig.setCreateTime(rs.getString("create_time"));
			}
			DBUtils.closeRs(rs);
			DBUtils.closeStmt(stmt);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		DBUtils.closeConn(con);
		return clusterConfig;
	}

}
